﻿#include "DockWidgetPropertyEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(DockWidget)

#include "BooleanEditEntry.h"
#include "FlagsEditEntry.h"
#include "EnumEditEntry.h"
#include "UiTextEditEntry.h"

struct DockWidgetPropertyEditEntry::DockWidgetPropertyEditEntryPrivate {
    QPointer<QDockWidget> object;

    QPointer<BooleanEditEntry> entryFloating;
    QPointer<FlagsEditEntry> entryFeatures;
    QPointer<FlagsEditEntry> entryAllowedAreas;
    QPointer<UiTextEditEntry> entryWindowTitle;
    QPointer<EnumEditEntry> entryDockWidgetArea;
    QPointer<BooleanEditEntry> entryDocked;
};

DockWidgetPropertyEditEntry::~DockWidgetPropertyEditEntry() { }

DockWidgetPropertyEditEntry::DockWidgetPropertyEditEntry(QWidget *parent) : PropertyEditEntry {parent} {
    p.reset(new DockWidgetPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

void DockWidgetPropertyEditEntry::setObject(void *object) {
    p->object = static_cast<QDockWidget*>(object);
}

void DockWidgetPropertyEditEntry::onTick() {
    if(!p->object || !isVisible()) {
        return ;
    }

    p->entryFloating->setValue(p->object->isFloating());
    p->entryFeatures->setValue(p->object->features());
    p->entryAllowedAreas->setValue(p->object->allowedAreas());
    p->entryWindowTitle->setValue(p->object->windowTitle());
    //p->entryDockWidgetArea->setValue(p->object->are);
    //p->entryDocked->setValue(p->object->isD);
}

void DockWidgetPropertyEditEntry::initUi() {
    p->entryFloating = new BooleanEditEntry;
    appendChildEntry(p->entryFloating);
    connect(p->entryFloating, &BooleanEditEntry::valueChanged, [=](bool checked) { p->object->setFloating(checked); });

    p->entryFeatures = new FlagsEditEntry;
    p->entryFeatures->setMetaEnum(QMetaEnum::fromType<DockWidgetPropertyEditEntry::DockWidgetFeature>());
    appendChildEntry(p->entryFeatures);
    connect(p->entryFeatures, &FlagsEditEntry::valueChanged, [=](int value) {
        p->object->setFeatures(QDockWidget::DockWidgetFeatures(value));
    });

    p->entryAllowedAreas = new FlagsEditEntry;
    p->entryAllowedAreas->setMetaEnum(QMetaEnum::fromType<Qt::DockWidgetArea>());
    appendChildEntry(p->entryAllowedAreas);
    connect(p->entryAllowedAreas, &FlagsEditEntry::valueChanged, [=](int value) {
        p->object->setAllowedAreas(Qt::DockWidgetAreas(value));
    });

    p->entryWindowTitle = new UiTextEditEntry;
    appendChildEntry(p->entryWindowTitle);
    connect(p->entryWindowTitle, &UiTextEditEntry::valueChanged, [=](const QString &text) { p->object->setWindowTitle(text); });

    p->entryDockWidgetArea = new EnumEditEntry;
    p->entryDockWidgetArea->setMetaEnum(QMetaEnum::fromType<Qt::DockWidgetArea>());
    appendChildEntry(p->entryDockWidgetArea);

    p->entryDocked = new BooleanEditEntry;
    appendChildEntry(p->entryDocked);
}

void DockWidgetPropertyEditEntry::retranslateUi() {
    setTitle(tr("QDockWidget"));

    p->entryFloating->setTitle(tr("floating"));
    p->entryFeatures->setTitle(tr("features"));
    p->entryAllowedAreas->setTitle(tr("allowedAreas"));
    p->entryWindowTitle->setTitle(tr("windowTitle"));
    p->entryDockWidgetArea->setTitle(tr("dockWidgetArea"));
    p->entryDocked->setTitle(tr("docked"));
}
